package com.tuanbiao.presenter;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.BlastDetailEntity;
import com.hzjq.data.hzjq.entity.LogEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.LogController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.events.BlastDetailsEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author abiao
 * @since 2022/4/28 11:44
 */
public class LogOperatePresenter extends BasePresenter<LogController.Model, LogController.View> implements LogController.Presenter {
    private BlastDetailEntity mDetailEntity;

    public LogOperatePresenter(LogController.Model model, LogController.View rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void init(BlastDetailsEvent event) {
        mDetailEntity = event.getEntity();
        EventBus.getDefault().removeStickyEvent(event);
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        if (null != mDetailEntity) {
            mRootView.setItems(mModel.getLogs(mDetailEntity.getBlastId()));
        }else {
            mModel.getLogs(new OnBaseResultCallback<List<LogEntity>>() {
                @Override
                public void onSuccess(List<LogEntity> logEntities) {
                    mRootView.setItems(logEntities);
                }
            });
        }
    }

}
